package com.aiti.lulian.strategy.task;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardUserIncreaseRankDto;
import com.aiti.lulian.dto.newCard.ExchangeCardParamDto;
import com.aiti.lulian.entity.CardRankTaskUserComplete;
import com.aiti.lulian.entity.CardUserRankTaskHistory;
import com.aiti.lulian.entity.newCard.CardBaseInfo;
import com.aiti.lulian.entity.newCard.CardDescription;
import com.aiti.lulian.entity.newCard.CardUserExtraUnion;
import com.aiti.lulian.enums.RankIncreaseTaskStrategyEnum;
import com.aiti.lulian.mapper.CardRankTaskUserCompleteMapper;
import com.aiti.lulian.mapper.newCard.CardDescriptionMapper;
import com.aiti.lulian.mapper.newCard.CardExchangeMapper;
import com.aiti.lulian.mapper.newCard.CardPersonalBaseInfoMapper;
import com.aiti.lulian.service.CardUserRankTaskHistoryService;
import com.aiti.lulian.vo.CardRankTaskUserVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class CompleteCardSendExchangeTaskStrategy implements RankIncreaseTaskStrategy {
    @Autowired
    private CardRankTaskUserCompleteMapper cardRankTaskUserCompleteMapper;
    @Autowired
    private CardUserRankTaskHistoryService cardUserRankTaskHistoryService;
    @Autowired
    private CardPersonalBaseInfoMapper cardBaseInfoMapper;
    @Autowired
    private CardExchangeMapper cardExchangeMapper;

    @Override
    public RankIncreaseTaskStrategyEnum getStrategy() {
        return RankIncreaseTaskStrategyEnum.COMPLETE_CARD_EXCHANGE;
    }

    @Override
    public void handleTask(CardRankTaskUserVo vo, CardUserIncreaseRankDto dto) {

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Message handle(CardUserIncreaseRankDto dto) {

        String cardId = dto.getCardId();
        Date date = new Date();
        DateTime beginOfDay = DateUtil.beginOfMonth(date);
        DateTime endOfDay = DateUtil.endOfMonth(date);


        LambdaQueryWrapper<CardUserRankTaskHistory> wrapper = new LambdaQueryWrapper<CardUserRankTaskHistory>().eq(CardUserRankTaskHistory::getCardId, cardId)
                .eq(CardUserRankTaskHistory::getType, RankIncreaseTaskStrategyEnum.COMPLETE_CARD_EXCHANGE.getType())
                .ge(CardUserRankTaskHistory::getBaseCreateTime, beginOfDay).le(CardUserRankTaskHistory::getBaseCreateTime, endOfDay);
        List<CardUserRankTaskHistory> list = cardUserRankTaskHistoryService.list(wrapper);
        if(!CollectionUtils.isEmpty(list)){
            long sumScore = list.stream().mapToLong(m -> m.getScore()).sum();
            if(sumScore >= 1000){
                return Message.success("每月封顶1000人气值");
            }

        }
        ExchangeCardParamDto exchangeDto = new ExchangeCardParamDto();
        exchangeDto.setFromCardId(cardId);
        exchangeDto.setToCardId(dto.getToCardId());
        exchangeDto.setEnable("1");
        exchangeDto.setExchangeSource("名企互联");
        List<CardUserExtraUnion> cardUserExtraUnions = cardExchangeMapper.queryExchangeList(exchangeDto);
        if(!CollectionUtils.isEmpty(cardUserExtraUnions)){

            Long score = 10L;
            dto.setScore(score);
            CardBaseInfo cardBaseInfo = getBaseInfoMapper().selectById(dto.getCardId());
            CardBaseInfo updateCardBaseInfo = new CardBaseInfo();
            updateCardBaseInfo.setBaseId(dto.getCardId());
            updateCardBaseInfo.setCardRankTotalScore(cardBaseInfo.getCardRankTotalScore() + dto.getScore());
            getBaseInfoMapper().updateById(updateCardBaseInfo);
            recordHistory(dto);

        }
        return Message.success();
    }

    @Override
    public CardRankTaskUserCompleteMapper getCardRankTaskUserCompleteMapper() {
        return cardRankTaskUserCompleteMapper;
    }

    @Override
    public CardPersonalBaseInfoMapper getBaseInfoMapper() {
        return cardBaseInfoMapper;
    }

    @Override
    public CardUserRankTaskHistoryService getHisotryService() {
        return cardUserRankTaskHistoryService;
    }


}